package logic

import (
	"context"
	"fmt"
	"qimao-fiction/model"

	"qimao-fiction/novelrpc/internal/svc"
	"qimao-fiction/novelrpc/novelrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *novelrpc.LoginRequest) (*novelrpc.LoginResponse, error) {
	// todo: add your logic here and delete this line

	var user model.User
	model.DB.Debug().Where("username=?", in.Username).Find(&user)
	if user.ID == 0 {
		return nil, fmt.Errorf("该用户未注册")
	}
	if in.Password != user.Password {
		return nil, fmt.Errorf("密码错误，请重新登录")
	}
	return &novelrpc.LoginResponse{
		UserId: int64(user.ID),
	}, nil
}
